﻿/// <reference path="../../common/common.js" />
window.indexPath = "../../";
(function () {
    var $ = fly;
    fly.mini.ajax = $.ajax;
    var treeContainer=$("#tree-container");
    var tree = new fly.mini.Tree({
        container: treeContainer[0],
        selectionMode: fly.mini.selectionMode.none,
        checkMode: fly.mini.checkMode.multi,
        checkCascade: {
            check: { parent: 0, children: true },
            uncheck: { parent: 0, children: true }
        },
        autoScroll: true,
        showBorder: false,
        itemKey: "id",
        navTarget: "iframe",
        toggleEvents: { panel: 'click' },
        onCheck: function (item, fireByDom) {
            if (window.frameElement.onCheck)
                window.frameElement.onCheck(item, fireByDom);
        },
        onExpand:function(item){
            if (window.frameElement.onExpand)
                window.frameElement.onExpand(item);
        },
        onJoin: function (item) {
            item.text = item.name+(item.phone?"-"+item.phone:'')+(item.orgName?"-"+item.orgName:'');
            item.iconCss = "tree-node-" + item.type
            item.leaf = item.type == "user"||item.type=="search"
        },
        onRequestData: function (item, result) {
            if (window.frameElement.onRequestData)
                window.frameElement.onRequestData(item,result);
            return result.data
        },
        async: {
            url: $.base.ajaxUrl("base", "UserTree", "id={id}&type={type}"),
            method: "post"
        }
    });

    var searchNode=null;
    function createSearchNode(){
        if(searchNode)
            return;
        searchNode= tree.root.insertItem(0,{name:'搜索结果',type:'search',leaf:true,iconCss:'tree-node-search'})[0];
    }

    function showSearchResult(items){
        createSearchNode()
        searchNode.removeItem(searchNode.items)
        searchNode.addItem(items);
        if(items.length)
            searchNode.expand();
        searchNode.setText("搜索结果({0})".format(items.length));
    }

    if ($.getQuery("supper-search")=="1") {
        var searchBox = $('<input id=search-box />');
        $.getBody().prepend(searchBox);
        
        new fly.ui.EmptyBox({ input: searchBox,emptyValue:"姓名、手机" });
        var previousSearchKey="";
        searchBox.keyup(function () {
            clearTimeout(this.searchHandler)
            this.searchHandler = setTimeout(function () {
                var key = searchBox.value().trim();
                if (key!=previousSearchKey) {
                    if(key){
                        $.post($.base.ajaxUrl("base","Search"),{type:'user',key:key},function(result){
                            previousSearchKey=key;
                            showSearchResult(result.data);
                        })
                    }
                    else{
                        showSearchResult([]);
                    }
                }
            }, 1000)
        })
    }

    if ($.getQuery("auto-height") == "1") {
        treeContainer.on("sizechange", function () {
            frameElement.style.height = (treeContainer.rect().height+ 50) + "px";
        })
    }
})();